angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope) {})

.controller('ChatsCtrl', function($scope, Chats) {
  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

// broadcast
.controller('SelfCtrl',['$rootScope', '$scope',function($scope,$rootScope) {
  $scope.click = function () {
    // $scope.$broadcast('to-child', 'child');
    $rootScope.$broadcast('to-child', 'child');
    $scope.$emit('to-parent', 'parent');
  }
}])

.controller('ParentCtrl', ['$rootScope','$scope',function($scope,$rootScope) {
  $scope.$on('to-parent', function(event,data) {
    console.log('ParentCtrl', data);       //父级能得到值
  });
  $scope.$on('to-child', function(event,data) {
    console.log('ParentCtrl', data);       //子级得不到值
  });
}])

.controller('ChildCtrl', ['$rootScope','$scope',function($scope,$rootScope){
  $scope.$on('to-child', function(event,data) {
    console.log('ChildCtrl', data);         //子级能得到值
  });
  $scope.$on('to-parent', function(event,data) {
    console.log('ChildCtrl', data);         //父级得不到值
  });
}])

.controller('BroCtrl', ['$rootScope','$scope',function($scope,$rootScope){
  $scope.$on('to-parent', function(event,data) {
    console.log('BroCtrl', data);          //平级得不到值
  });
  $scope.$on('to-child', function(event,data) {
    console.log('BroCtrl', data);          //平级得不到值
  });
}])


/**
 * 前缀传值
 */
  .controller("MyController", function ($scope) {
    //J1 这里我们在作用域里初始化两个变量
    $scope.name = "dreamapple";
    $scope.age = 20;
    $scope.changeAge = function(){
      $scope.age = 0;
    }
  })



.controller('AccountCtrl', function($scope) {
  $scope.settings = {
    enableFriends: true
  };
})

.directive("myDirective", function () {
 /* var obj = {
    //J4   指令的声明模式为 "AE" 属性和元素
    restrict: "AE",
    //J5   指令继承父作用域的属性和方法
    scope: {},
    replace: true,
    template: "<div class='my-directive'>" +
    "<h3>下面部分是我们创建的指令生成的</h3>" +
    "我的名字是：<span ng-bind='name'></span><br/>" +
    "我的年龄是：<span ng-bind='age'></span>" +
    "<input type='text' ng-model='name'>" +
    " </div>"
  }
  return obj;*/
  var obj = {
    restrict: "AE",
    scope: {
      name: '@myName',
      age: '=',
      changeAge: '&changeMyAge'
    },
    replace: true,
    template: "<div class='my-directive'>" +
    "<h3>下面部分是我们创建的指令生成的</h3>" +
    "我的名字是：<span ng-bind='name'></span><br/>" +
    "我的年龄是：<span ng-bind='age'></span><br/>" +
    "在这里修改名字：<input type='text' ng-model='name'><br/>" +
    "<button ng-click='changeAge()'>修改年龄</button>" +
    " </div>"
  }
  return obj;
});
